package cn.kgc.mes.storage.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.kgc.mes.common.annotation.Log;
import cn.kgc.mes.common.core.controller.BaseController;
import cn.kgc.mes.common.core.domain.AjaxResult;
import cn.kgc.mes.common.enums.BusinessType;
import cn.kgc.mes.storage.domain.RtIssueLine;
import cn.kgc.mes.storage.service.IRtIssueLineService;
import cn.kgc.mes.common.utils.poi.ExcelUtil;
import cn.kgc.mes.common.core.page.TableDataInfo;

/**
 * 生产退料单行Controller
 * 
 * @author ydw
 * @date 2025-09-15
 */
@RestController
@RequestMapping("/line/line")
public class RtIssueLineController extends BaseController
{
    @Autowired
    private IRtIssueLineService rtIssueLineService;

    /**
     * 查询生产退料单行列表
     */
//    @PreAuthorize("@ss.hasPermi('line:line:list')")
    @GetMapping("/list")
    public TableDataInfo list(RtIssueLine rtIssueLine)
    {
        startPage();
        List<RtIssueLine> list = rtIssueLineService.selectRtIssueLineList(rtIssueLine);
        return getDataTable(list);
    }

    /**
     * 导出生产退料单行列表
     */
//    @PreAuthorize("@ss.hasPermi('line:line:export')")
    @Log(title = "生产退料单行", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RtIssueLine rtIssueLine)
    {
        List<RtIssueLine> list = rtIssueLineService.selectRtIssueLineList(rtIssueLine);
        ExcelUtil<RtIssueLine> util = new ExcelUtil<RtIssueLine>(RtIssueLine.class);
        util.exportExcel(response, list, "生产退料单行数据");
    }

    /**
     * 获取生产退料单行详细信息
     */
//    @PreAuthorize("@ss.hasPermi('line:line:query')")
    @GetMapping(value = "/{lineId}")
    public AjaxResult getInfo(@PathVariable("lineId") Long lineId)
    {
        return success(rtIssueLineService.selectRtIssueLineByLineId(lineId));
    }

    /**
     * 新增生产退料单行
     */
//    @PreAuthorize("@ss.hasPermi('line:line:add')")
    @Log(title = "生产退料单行", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RtIssueLine rtIssueLine)
    {
        return toAjax(rtIssueLineService.insertRtIssueLine(rtIssueLine));
    }

    /**
     * 修改生产退料单行
     */
//    @PreAuthorize("@ss.hasPermi('line:line:edit')")
    @Log(title = "生产退料单行", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RtIssueLine rtIssueLine)
    {
        return toAjax(rtIssueLineService.updateRtIssueLine(rtIssueLine));
    }

    /**
     * 删除生产退料单行
     */
//    @PreAuthorize("@ss.hasPermi('line:line:remove')")
    @Log(title = "生产退料单行", businessType = BusinessType.DELETE)
	@DeleteMapping("/{lineIds}")
    public AjaxResult remove(@PathVariable Long[] lineIds)
    {
        return toAjax(rtIssueLineService.deleteRtIssueLineByLineIds(lineIds));
    }
//    根据rtId查询
    @GetMapping("list1")
    public Object list1(@RequestParam("rtId") Long rtId){
        List<RtIssueLine> list = rtIssueLineService.selectRtIssueLineList1(rtId);
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("code",200);
            map.put("msg","查询成功");
            map.put("data",list);
        }else{
            map.put("code",0);
            map.put("msg","查询失败");
        }
        return map;
    }
    //根据id修改库存量
    @PostMapping("update1")
    public Object update(@RequestParam("materialStockId") Integer materialStockId,
                         @RequestParam("quantityOnhand") Integer quantityOnhand){
        int i = rtIssueLineService.update1(materialStockId, quantityOnhand);
        Map<String,Object> map=new HashMap<>();
        if(i>0){
            map.put("code",200);
            map.put("msg","修改成功");
        }else{
            map.put("code",500);
            map.put("msg","修改失败");
        }
        return  map;
    }
    //根据rtId将表中对应的所有数据都删除
    @PostMapping("/deleLine")
    public Object deleteLine(@RequestBody Long[] rtId) {
        int i = rtIssueLineService.deleteWmIssueLineByIssueId(rtId);
        Map<String, Object> map = new HashMap<>();
        if(i>0){
            map.put("code",200);
            map.put("msg","删除成功");
        }else{
            map.put("code",0);
            map.put("msg","删除失败");
        }
        return map;
    }
}
